package com.wing.gateway.utils;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * @author
 */
@ConfigurationProperties(prefix = "wing.oauth2.ignored")
public class PermitUrlProperties {

	/**
	 * 监控中心和swagger需要访问的url
	 */
	private static final String[] ENDPOINTS = {
			//knife4j放行规则
			"/doc.html","/v3/api-docs/**", "/webjars/**", "/swagger-resources/**"
	};

	private String[] url;

	private Map<String, Server> server = new LinkedHashMap();


	/**
	 * 需要放开权限的url
	 *
	 * @return
	 */
	public String[] getIgnoredUrl() {
		if (url == null || url.length == 0) {
			return ENDPOINTS;
		}

		List<String> list = new ArrayList<>();
		for (String u : ENDPOINTS) {
			list.add(u);
		}
		for (String u : url) {
			list.add(u);
		}

		return list.toArray(new String[list.size()]);
	}

	public void setUrl(String[] url) {
		this.url = url;
	}

	/**
	 * 需要放开权限的url
	 *
	 * @return
	 */
	public String[] getIgnoredServerUrl() {
		if (server == null || server.size() == 0) {
			return ENDPOINTS;
		}

		List<String> list = new ArrayList<>();
		for (String url : ENDPOINTS) {
			list.add(url);
		}

		Iterator var1 = this.server.entrySet().iterator();
		while(var1.hasNext()) {
			Map.Entry<String, Server> entry = (Map.Entry)var1.next();
			Server value = entry.getValue();
			if (value != null && !StringUtils.isEmpty(value.getUrl())) {
				String[] urls = value.getUrl().split(",");
				for (String url : urls) {
					list.add(value.getPrefix() + url);
				}
			}
		}

		return list.toArray(new String[list.size()]);
	}

	public void setServer(Map<String, Server> server) {
		this.server = server;
	}


	public static class Server {
		private String prefix;
		private String url;

		public Server() {
		}

		public Server(String prefix, String url) {
			this.prefix = prefix;
			this.url = url;
		}

		public String getPrefix() {
			return prefix;
		}

		public void setPrefix(String prefix) {
			this.prefix = prefix;
		}

		public String getUrl() {
			return url;
		}

		public void setUrl(String url) {
			this.url = url;
		}
	}



}
